﻿<?php

function connectDB() {
	$sql = mysql_connect("localhost", "matuldo", "xpert26");
	if(!$sql) return false;
	if(!mysql_select_db("hn_matuldo", $sql)) return false;
	mysql_query("SET NAMES UTF8", $sql);
	return $sql;
}

function disconnectDB($sql) {
	if(isset($sql)) mysql_close($sql);
}

function hasChild($ar, $ind) {
	if(!is_array($ar)) return false;
	$pomArr = array();
	$j = 0;
	for($i = 0; $i < count($ar); $i++) {
		if($ar[$i]["parent"] == $ind) {
			$pomArr[$j] = $ar[$i];
			$j++;
		}
	}
	if(count($pomArr) < 1) return false;
		else return $pomArr;
}

function genSubMenu($menu, $arrayOfLinks) {
	for($i = 0; $i < count($arrayOfLinks); $i++) {
		$childs = hasChild($menu, $arrayOfLinks[$i]["id"]);
		if(!$childs) {
			echo "\t<li>";
			if($arrayOfLinks[$i]["link"] != "" && $arrayOfLinks[$i]["link"] != "NULL") echo "<a href='".$arrayOfLinks[$i]["link"]."'>";
			echo $arrayOfLinks[$i]["name"];
			if($arrayOfLinks[$i]["link"] != "" && $arrayOfLinks[$i]["link"] != "NULL") echo "</a>";
			echo "</li>\n";
		} else {
			echo "\t<li>".$arrayOfLinks[$i]["name"]."\n";
			echo "\t<ul>\n";
			genSubMenu($menu, $childs);
			echo "\t</ul>\n\t</li>\n";
		}
	}
}

function generateDevices($devicetypes, $location = "") {
	for($i = 0; $i < count($devicetypes); $i++) {
		echo "<li><a href='devices.php?type=".$devicetypes[$i]["id"]."&loc=".$location."'>".$devicetypes[$i]["name"]."</a></li>\n";
	}
}

function generateLeftMenu() {
	//if(!$sql) die("Nepodarilo sa spojit s databazou!");

	$query = "SELECT * FROM devicetypes";
	$res = mysql_query($query/*, $sql*/);
	if(!$res) die("Zlyhal select z DB");
	$devices = array();
	for($i = 0; $row = mysql_fetch_assoc($res); $i++) {
		$devices[$i]["id"] = $row["id"];
		$devices[$i]["name"] = $row["name"];
	}
	$query = "SELECT * FROM locations";
	$res = mysql_query($query/*, $sql*/);
	if(!$res) die("Zlyhal select z DB");
	$locations = array();
	for($i = 0; $row = mysql_fetch_assoc($res); $i++) {
		$locations[$i]["id"] = $row["id"];
		$locations[$i]["name"] = $row["name"];
	}

	$query = "SELECT * FROM menulinks";
	$res = mysql_query($query/*, $sql*/);
	if(!$res) die("Zlyhal select z DB");
		
	$menu = array();
	$i = 0;
	while($row = mysql_fetch_assoc($res)) {
		switch($row["id"]) {
		case 1: {
			echo "<li>".$row["name"]."\n<ul>";
			generateDevices($devices);
			echo "\n</ul>\n</li>";
			break;
		}
		case 2: {
			echo "<li>".$row["name"]."\n<ul>";
			for($j = 0; $j < count($locations); $j++) {
				echo "<li>".$locations[$j]["name"]."\n<ul>";
				generateDevices($devices, $locations[$j]["id"]);
				echo "\n</ul>\n</li>";
			}
			echo "\n</ul>\n</li>";
			break;
		} 
		default: {
			$menu[$i]["name"] = $row["name"];
			$menu[$i]["link"] = $row["link"];
			$menu[$i]["id"] = $row["id"];
			$menu[$i]["parent"] = $row["parent"];
			$i++;
		}
		}
	}

	$childs = hasChild($menu, 0);
	genSubMenu($menu, $childs);
} 

function getName($what, $id){
	$query = "SELECT name FROM ".$what." WHERE id = ".mysql_real_escape_string($id);
	$res = mysql_query($query);
	if(!$res) die("Zlyhal select z DB 1");
	
	$name = mysql_fetch_row($res);
	$name = $name[0];
	if(!$name) {
		$query = "SELECT name FROM ".$what." ORDER BY id DESC LIMIT 1";
		$res = mysql_query($query);
		if(!$res) die("Zlyhal select z DB 2");
		$name = mysql_fetch_row($res);
		$name = $name[0];
	}
	
	return $name;
}


?>